package com.sola.mybatis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sola.mybatis.entity.User;
import com.sola.mybatis.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/mybatis")
@Api("user相关的API,用于测试mybatis")
public class UserController {

	@Autowired
	private IUserService userService;
	
	@RequestMapping( method = RequestMethod.GET)
	@ApiOperation("查找所有User")
	public List<User> list(){
		return userService.getUserList();
	}
	
	@RequestMapping(value = "{id}" , method = RequestMethod.GET)
	@ApiOperation("通過id查找User")
	public User getUserById(@PathVariable String id){
		return userService.getUserById(id);
	}
	
	@RequestMapping(method = RequestMethod.PATCH)
	@ApiOperation("修改User")
	public User update(){
		User user = new User();
		String id = "1231311012";
		user.setId(id);
		user.setName("添加绝地");
		userService.update(user);
		return userService.getUserById(id);
	}
	
	@RequestMapping(value = "{id}" , method = RequestMethod.DELETE)
	@ApiOperation("通過id删除User")
	public String delete(@PathVariable String id){
		int count = userService.delete(id);
		return count + "";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation("增加一个User")
	public User add(@RequestBody User user){
		userService.add(user);
		return user;
	}
	
	
}
